package com.study.shop.config.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户拦截器
 */
@Component
@Slf4j
public class UserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 放行预检请求
//        if ("OPTIONS".equals(request.getMethod())) {
//            return true;
//        }
//        log.info("请求路径：{}，请求方式：{}", request.getRequestURI(), request.getMethod());
//        String token = request.getHeader("token");
//        // 2. 验证token
//        if (!ObjectUtils.isEmpty(token)) {
//            return true;
//        }
//        response.setCharacterEncoding("utf-8");
//        response.setContentType(MimeTypeUtils.APPLICATION_JSON_VALUE);
//
//        PrintWriter writer = response.getWriter();
//        R<Object> r = R.error("请先进行登陆再进行操作");
//        String jsonStr = JSONUtil.toJsonStr(r);
//        writer.write(jsonStr);
//        writer.flush();
//        writer.close();
//        return false;
        return true;
    }
}
